#!/bin/sh

e2e_exclusive_lock() {
  true
}

e2e_test() {
  run_test "Check that exporter queries are wokring" check_exporter_queries
  run_test "Check that collector was created" check_collector
  run_test "Check that collector pod monitor was created" check_collector_pod_monitor
  run_test "Check that grafana is embedded" check_grafana_embedded
  run_test "Check that grafana secret is used" check_grafana_secret
}

e2e_test_uninstall() {
  helm_cleanup_chart "$CLUSTER_NAME" "$CLUSTER_NAMESPACE"
  k8s_async_cleanup_namespace "$CLUSTER_NAMESPACE"
  uninstall_prometheus_operator
}

check_grafana_secret() {
  upgrade_operator \
    --set grafana.autoEmbed=true \
    --set-string grafana.webHost="prometheus-grafana.$(prometheus_namespace)" \
    --set grafana.secretNamespace="$(prometheus_namespace)" \
    --set grafana.secretName="prometheus-grafana" \
    --set grafana.secretUserKey="admin-user" \
    --set grafana.secretPasswordKey="admin-password"

  local EXIT_CODE RESULT
  try_function wait_until kubectl exec -i -n "$CLUSTER_NAMESPACE" curl -- curl -s -k "https://stackgres-restapi.$OPERATOR_NAMESPACE/grafana" -o /dev/null --fail
  if "$RESULT"
  then
    success "Grafana URL can be retrieved"
  else
    fail "Grafana URL can not be retrieved"
  fi
}

check_exporter_queries() {
  local METRIC
  local QUERIES_FILE=queries.yaml
  local VERSION_AS_NUMBER VERSION_1_15_0
  VERSION_AS_NUMBER="$(get_version_as_number "${STACKGRES_TARGET_VERSION:-$STACKGRES_VERSION}")"
  VERSION_1_15_0="$(get_version_as_number 1.15.0-SNAPSHOT)"
  if [ "$VERSION_AS_NUMBER" -lt "$VERSION_1_15_0" ]
  then
    QUERIES_FILE=queries-1.23.yaml
  fi
  for METRIC in $(yq -r 'keys[]' "$PROJECT_PATH"/stackgres-k8s/src/operator/src/main/resources/prometheus-postgres-exporter/"$QUERIES_FILE")
  do
    QUERY="$(yq -r ".[\"$METRIC\"].query" "$PROJECT_PATH"/stackgres-k8s/src/operator/src/main/resources/prometheus-postgres-exporter/"$QUERIES_FILE")"
    if wait_until eval 'kubectl exec -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-0" -c postgres-util -- \
      psql -q -v ON_ERROR_STOP=ON -c "$QUERY" > /dev/null'
    then
      echo "Exporter metric $METRIC query is working"
    else
      echo "Exporter metric $METRIC query is failing"
      return 1
    fi
  done
}

check_collector() {
  local SERVICE_MONITOR="stackgres-collector"

  if wait_until kubectl get deployment -n "$OPERATOR_NAMESPACE" "$SERVICE_MONITOR"
  then
    echo "Collector deployment was created"
  else
    echo "Collector deployment wasn't created"
    return 1
  fi
}

check_collector_pod_monitor() {
  local SERVICE_MONITOR="stackgres-collector"

  if wait_until kubectl get podmonitors.monitoring.coreos.com -n "$OPERATOR_NAMESPACE" "$SERVICE_MONITOR"
  then
    echo "Collector pod monitor was created"
  else
    echo "Collector pod monitor wasn't created"
    return 1
  fi

  if wait_until eval "[ \"\$(kubectl exec -i -n '$CLUSTER_NAMESPACE' curl \
    -- curl -s -f 'http://$(prometheus_service).$(prometheus_namespace):9090/api/v1/query?query=pg_settings_shared_buffers_bytes' \
    | jq -r '.data.result | length')\" -gt 0 ]"
  then
    echo "Exporter metric pg_settings_shared_buffers_bytes was found on prometheus"
  else
    echo "Exporter metric pg_settings_shared_buffers_bytes wasn't found on prometheus"
    return 1
  fi

  if wait_until eval "[ \"\$(kubectl exec -i -n '$CLUSTER_NAMESPACE' curl \
    -- curl -s -f 'http://$(prometheus_service).$(prometheus_namespace):9090/api/v1/query?query=pg_stat_activity_count' \
    | jq -r '.data.result | length')\" -gt 0 ]"
  then
    echo "Exporter metric pg_stat_activity_count was found on prometheus"
  else
    echo "Exporter metric pg_stat_activity_count wasn't found on prometheus"
    return 1
  fi
}

check_grafana_embedded() {
  if wait_until kubectl exec -i -n "$CLUSTER_NAMESPACE" curl -- curl -s -k "https://stackgres-restapi.$OPERATOR_NAMESPACE/grafana" -o /dev/null --fail
  then
    success "Grafana URL can be retrieved"
  else
    fail "Grafana URL can not be retrieved"
  fi

  GRAFANA_URL="$(kubectl exec -i -n "$CLUSTER_NAMESPACE" curl -- curl -s -k "https://stackgres-restapi.$OPERATOR_NAMESPACE/grafana" --fail)"

  if wait_until kubectl exec -i -n "$CLUSTER_NAMESPACE" curl -- curl -s -k "$GRAFANA_URL" -o /dev/null --fail
  then
    success "Grafana URL $GRAFANA_URL is accessible"
  else
    fail "Grafana URL $GRAFANA_URL is not accessible"
  fi

  if wait_until eval 'kubectl exec -i -n "$CLUSTER_NAMESPACE" curl -- curl -s -k "$GRAFANA_URL" --fail | grep -q "\(<grafana-app \|<title>Grafana</title>\)"'
  then
    success "Grafana URL $GRAFANA_URL is returning grafana Web"
  else
    fail_no_return "Grafana URL $GRAFANA_URL is not returning grafana Web"
    kubectl exec -i -n "$CLUSTER_NAMESPACE" curl -- curl -s -k "$GRAFANA_URL" --fail
    return 1
  fi

  if [ "$(kubectl get deployment -n "$OPERATOR_NAMESPACE" stackgres-operator --template '{{ len .spec.template.spec.containers }}')" -ne 1 ]
  then
    echo "Skipping check since operator environment variable can not be changed when operator is not deployed in kubernetes"
    return
  fi
}
